from sqlalchemy import Column, Integer, String, Index
from app.db.base import Base


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, autoincrement=True)  # MySQL 自增主键
    name = Column(String(50), nullable=False)  # 明确字符串长度（MySQL 推荐）
    email = Column(String(100), unique=True, nullable=False)  # 唯一邮箱

    # 索引优化查询
    __table_args__ = (
        Index("idx_user_name", "name"),  # 用户名索引
        Index("idx_user_email", "email"),  # 邮箱索引
    )

# 关联物品（一对多）
from sqlalchemy.orm import relationship
items = relationship("Item", back_populates="owner", cascade="all, delete-orphan")